import 'dart:math';

class Countdown {
  int? countdownSec;

  int? PSCardcountdownSec;

  int? PSCardRemPrice;

  int? initTimeSinceEpoch;
  Countdown({this.countdownSec = 0});

  int get leftTimeSinceEpoch {
    if (initTimeSinceEpoch != null && countdownSec != null) {
      int curTimeSE = DateTime.now().millisecondsSinceEpoch ~/ 1000;
      return max(0, countdownSec! - (curTimeSE - initTimeSinceEpoch!));
    }
    return 0;
  }

  String get secondsDesc => (leftTimeSinceEpoch % 60).toString().padLeft(2, '0');

  String get minDesc => ((leftTimeSinceEpoch ~/ 60) % 60).toString().padLeft(2, '0');

  String get hourDesc => (leftTimeSinceEpoch ~/ 3600).toString().padLeft(2, '0');

  Countdown.fromJson(Map<String, dynamic>? json) {
    json ??= {};
    countdownSec = json['countdownSec'] ?? 0;
    PSCardcountdownSec = json['PSCardcountdownSec'] ?? 0;
    PSCardRemPrice = json['PSCardRemPrice'] ?? 0;
    initTimeSinceEpoch = DateTime.now().millisecondsSinceEpoch ~/ 1000;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = {};
    data['countdownSec'] = countdownSec;
    data['PSCardcountdownSec'] = PSCardcountdownSec;
    data['PSCardRemPrice'] = PSCardRemPrice;
    return data;
  }
}
